#include "NewUpdateChannelDialog.h"
#include "ui_NewUpdateChannelDialog.h"
#include "ProtocolDialog.h"
#include <QDir>

NewUpdateChannelDialog::NewUpdateChannelDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NewUpdateChannelDialog)
{
    ui->setupUi(this);
    setWindowTitle("新建转发通道");

    QWidget * tcpClientWidget = new QWidget();
    QVBoxLayout * vLayout = new QVBoxLayout();
    QHBoxLayout * hLayout = new QHBoxLayout();
    {
        QLabel * label1 = new QLabel("IP地址A");
        QLineEdit * edit1 = new QLineEdit();
        m_mapChilds.insert("ipaddrA",edit1);
        QLabel * label2 = new QLabel("端口号A");
        QLineEdit * edit2 = new QLineEdit();
        m_mapChilds.insert("portA",edit2);
        hLayout->addWidget(label1);
        hLayout->addWidget(edit1);
        hLayout->addWidget(label2);
        hLayout->addWidget(edit2);
        vLayout->addLayout(hLayout);
    }
    QHBoxLayout * hLayout1 = new QHBoxLayout();
    {
        QLabel * label1 = new QLabel("IP地址B");
        QLineEdit * edit1 = new QLineEdit();
        m_mapChilds.insert("ipaddrB",edit1);
        QLabel * label2 = new QLabel("端口号B");
        QLineEdit * edit2 = new QLineEdit();
        m_mapChilds.insert("portB",edit2);
        hLayout1->addWidget(label1);
        hLayout1->addWidget(edit1);
        hLayout1->addWidget(label2);
        hLayout1->addWidget(edit2);
        vLayout->addLayout(hLayout1);
    }
    tcpClientWidget->setLayout(vLayout);
    ui->stackedWidget->addWidget(tcpClientWidget);
    //tcpServer配置
    QWidget * tcpServerWidget = new QWidget();
    QHBoxLayout * hLayout3 = new QHBoxLayout();
    {
        QLabel * label1 = new QLabel("服务端IP地址");
        QLineEdit * edit1 = new QLineEdit();
        m_mapChilds.insert("serverip",edit1);
        QLabel * label2 = new QLabel("服务端端口号");
        QLineEdit * edit2= new QLineEdit();
        m_mapChilds.insert("serverport",edit2);
        hLayout3->addWidget(label1);
        hLayout3->addWidget(edit1);
        hLayout3->addWidget(label2);
        hLayout3->addWidget(edit2);
    }
    tcpServerWidget->setLayout(hLayout3);
    ui->stackedWidget->addWidget(tcpServerWidget);

    //UDP配置
    QWidget * udpWidget = new QWidget();
    QVBoxLayout * vLayout6 = new QVBoxLayout();
    {
        QHBoxLayout * hLayout4 = new QHBoxLayout();
        QHBoxLayout * hLayout5 = new QHBoxLayout();
        QLabel * label1 = new QLabel("远程IP");
        QLineEdit * edit1 = new QLineEdit();
        m_mapChilds.insert("updDestIpaddr",edit1);
        QLabel * label2 = new QLabel("远程端口");
        QLineEdit * edit2= new QLineEdit();
        m_mapChilds.insert("updDestPort",edit2);
        QLabel * label3 = new QLabel("本地IP");
        QLineEdit * edit3 = new QLineEdit();
        m_mapChilds.insert("updSrcIpaddr",edit3);
        QLabel * label4 = new QLabel("本地端口");
        QLineEdit * edit4= new QLineEdit();
        m_mapChilds.insert("updSrcPort",edit4);

        hLayout4->addWidget(label1);
        hLayout4->addWidget(edit1);
        hLayout4->addWidget(label2);
        hLayout4->addWidget(edit2);

        hLayout5->addWidget(label3);
        hLayout5->addWidget(edit3);
        hLayout5->addWidget(label4);
        hLayout5->addWidget(edit4);

        vLayout6->addLayout(hLayout4);
        vLayout6->addLayout(hLayout5);
    }
    udpWidget->setLayout(vLayout6);
    ui->stackedWidget->addWidget(udpWidget);
    //串口
    QWidget * commWidget = new QWidget();
    QHBoxLayout * hLayout7 = new QHBoxLayout();
    {
        QLabel * label1 = new QLabel("串口号");
        QLineEdit * edit1 = new QLineEdit();
        m_mapChilds.insert("comName",edit1);
        QLabel * label2 = new QLabel("波特率");
        QComboBox * combox = new QComboBox();
        combox->addItems(QStringList{"4800","9600","14400","19200","115200"});
        combox->setCurrentIndex(1);
         m_mapChilds.insert("baudRate",combox);
        QLabel * label3 = new QLabel("数据位");
        QComboBox * combox1 = new QComboBox();
        combox1->addItems(QStringList{"4","5","6","7","8"});
        combox1->setCurrentIndex(3);
        m_mapChilds.insert("dataBit",combox1);
        QLabel * label4 = new QLabel("检验位");
        QComboBox * combox2 = new QComboBox();
        combox2->addItems(QStringList{"奇","偶","无"});
         m_mapChilds.insert("checkBit",combox2);
        QLabel * label5 = new QLabel("停止位");
        QComboBox * combox3 = new QComboBox();
        combox3->addItems(QStringList{"1","1.5","2"});
         m_mapChilds.insert("stopBit",combox3);
        hLayout7->addWidget(label1);
        hLayout7->addWidget(edit1);
        hLayout7->addWidget(label2);
        hLayout7->addWidget(combox);
        hLayout7->addWidget(label3);
        hLayout7->addWidget(combox1 );
        hLayout7->addWidget(label4 );
        hLayout7->addWidget(combox2 );
        hLayout7->addWidget(label5 );
        hLayout7->addWidget(combox3 );
    }
    commWidget->setLayout(hLayout7);
    ui->stackedWidget->addWidget(commWidget);
}

NewUpdateChannelDialog::~NewUpdateChannelDialog()
{
    delete ui;
}

void NewUpdateChannelDialog::SetInitChannelName(QString updateName)
{
    ui->lineEdit->setText(updateName);
    ui->lineEdit_2->setText("转发通道");
}

stUpdateChannel NewUpdateChannelDialog::GetUpdateChannelInfo()
{
    stUpdateChannel updateInfo;
    updateInfo.name = ui->lineEdit->text().toStdString();
    updateInfo.desc = ui->lineEdit_2->text().toStdString();
    updateInfo.protoCate = ui->lineEdit_5->text().toStdString();
    updateInfo.protoName = ui->lineEdit_6->text().toStdString();

    updateInfo.periodTime = ui->comboBox_3->currentText().toStdString();
    updateInfo.devAddr = ui->comboBox->currentText().toStdString();
    updateInfo.isDisable = ui->checkBox->checkState()?1:0;
    updateInfo.intarfaceType =(stUpdateChannel::Intarface_Type) ui->comboBox_2->currentIndex();

    updateInfo.tcpClient.ipaddrA = ((QLineEdit *)m_mapChilds["ipaddrA"])->text().toStdString();
    updateInfo.tcpClient.portA = ((QLineEdit *)m_mapChilds["portA"])->text().toStdString();
    updateInfo.tcpClient.ipaddrB = ((QLineEdit *)m_mapChilds["ipaddrB"])->text().toStdString();
    updateInfo.tcpClient.portB = ((QLineEdit *)m_mapChilds["portB"])->text().toStdString();

    updateInfo.tcpServer.ipaddr = ((QLineEdit *)m_mapChilds["serverip"])->text().toStdString();
    updateInfo.tcpServer.port = ((QLineEdit *)m_mapChilds["serverport"])->text().toStdString();

    updateInfo.udp.localIpaddr = ((QLineEdit *)m_mapChilds["updSrcIpaddr"])->text().toStdString();
    updateInfo.udp.localPort = ((QLineEdit *)m_mapChilds["updSrcPort"])->text().toStdString();
    updateInfo.udp.remoteIpaddr = ((QLineEdit *)m_mapChilds["updDestIpaddr"])->text().toStdString();
    updateInfo.udp.remotePort = ((QLineEdit *)m_mapChilds["updDestPort"])->text().toStdString();

    updateInfo.serialport.name = ((QLineEdit *)m_mapChilds["comName"])->text().toStdString();
    updateInfo.serialport.baudRate = ((QComboBox *)m_mapChilds["baudRate"])->currentText().toStdString();
    updateInfo.serialport.dataBit = ((QComboBox *)m_mapChilds["dataBit"])->currentText().toStdString();
    updateInfo.serialport.stopBit = ((QComboBox *)m_mapChilds["stopBit"])->currentText().toStdString();
    updateInfo.serialport.checkBit = ((QComboBox *)m_mapChilds["checkBit"])->currentText().toStdString();

    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        stPrivateParam param;
        param.id = ui->tableWidget->item(i,0)->text().toInt();
        param.name = ui->tableWidget->item(i,1)->text().toStdString();
        param.desc = ui->tableWidget->item(i,2)->text().toStdString();
        param.value = ui->tableWidget->item(i,3)->text().toStdString();
        updateInfo.params.push_back(param);
    }
    return updateInfo;
}

void NewUpdateChannelDialog::on_pushButton_clicked()
{
    accept();
}

void NewUpdateChannelDialog::on_pushButton_2_clicked()
{
    close();
}

void NewUpdateChannelDialog::on_comboBox_2_currentIndexChanged(int index)
{
    ui->stackedWidget->setCurrentIndex(index);
}

void NewUpdateChannelDialog::on_pushButton_3_clicked()
{
    ProtocolDialog protocalDialog;
    protocalDialog.SetProtocalType(ProtocolDialog::Protocal_Type::Update_Type);
    if(protocalDialog.exec()==QDialog::Accepted)
    {
        ui->lineEdit_5->setText(protocalDialog.GetSelectProtoName());
        ui->lineEdit_6->setText(protocalDialog.GetSelectProtoCate());
    }
}
void NewUpdateChannelDialog::ParseXML(QString strFilePath)
{
    pugi::xml_document doc;
    if(!doc.load_file(strFilePath.toStdString().c_str()))
    {
        return ;
    }
    pugi::xml_node collect_node = doc.root().child("protocal");
    pugi::xml_node params_node = collect_node.child("params");
    pugi::xml_node param_node = params_node.first_child();
    while(param_node)
    {
        stPrivateParam param ;
        param.id = param_node.attribute("id").as_int();
        param.desc = param_node.attribute("desc").value();
        param.name = param_node.attribute("name").value();
        param.value = param_node.attribute("value").value();
        m_params[param.id] = param;
        param_node = param_node.next_sibling();
    }
}

void NewUpdateChannelDialog::on_lineEdit_6_textChanged(const QString &arg1)
{
    QString path = QDir::currentPath()+"/Update/"+ arg1 +"/protocal.xml";
    m_params.clear();
    ParseXML(path);
    ui->tableWidget->horizontalHeader();
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->setRowCount(m_params.size());
    QMap<int,stPrivateParam>::iterator it = m_params.begin();
    for(int row = 0 ; it != m_params.end(); it++,row++)
    {
        QTableWidgetItem * pItem1 = new QTableWidgetItem(QString::number(it->id));
        pItem1->setFlags(Qt::ItemIsEnabled);
        pItem1->setBackgroundColor(QColor(192,192,192));
        ui->tableWidget->setItem(row,0, pItem1);
        QTableWidgetItem * pItem2 = new QTableWidgetItem(it->name.c_str());
        pItem2->setFlags(Qt::ItemIsEnabled);
        pItem2->setBackgroundColor(QColor(192,192,192));
        ui->tableWidget->setItem(row,1, pItem2);
        QTableWidgetItem * pItem3 = new QTableWidgetItem(it->desc.c_str());
        pItem3->setFlags(Qt::ItemIsEnabled);
        pItem3->setBackgroundColor(QColor(192,192,192));
        ui->tableWidget->setItem(row,2,pItem3);

        ui->tableWidget->setItem(row,3,new QTableWidgetItem(it->value.c_str()));
    }
}
